package com.victor.controller.web;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.log.Log;
import com.victor.service.LogService;
import com.victor.utils.DateUtils;
import com.victor.utils.Msg;

/**
 * 操作日志
 * 
 * @author zhangbin
 *
 */
@RestController
@RequestMapping("/log")
public class LogController {

	@Autowired
	private LogService logservice;

	/**
	 * 操作日志
	 * 
	 */

	// 添加操作日志
	// Log log = new Log();
	// log.setDate(DateUtils.getNowDate());
	// log.setTime(DateUtils.getNowTime());
	// log.setAdminid(adminid);
	// log.setContent("添加一个服务伙伴");
	// logservice.addLog(log);
	
	public void addLog(String adminid,String content,String type) {
		 	 Log log = new Log();
			 log.setDate(DateUtils.getNowDate());
			 log.setTime(DateUtils.getNowTime());
			 log.setAdminid(adminid);
			 log.setContent(content);
			 log.setType(type);
			 logservice.addLog(log);
	}
	
	
	// 获取操作日志列表
	@RequestMapping("getAllLogs")
	public Msg getAllLogs() {
		List<Log> logs = logservice.getAllLogs();
		return Msg.success().add("logs", logs);
	}

	// 批量导出操作日志
	@RequestMapping("batchExportUserLog")
	public void batchExportUserLog(String ulStarttime, String ulEndtime, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		// 数据库查询数据 exportlist 需要导出的list
		List<Log> logs = logservice.getAllLogs();

		// 创建poi导出数据对象
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		// 创建sheet页
		Sheet sheet = sxssfWorkbook.createSheet("操作日志");

		CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 6);
		// 参数1：起始行 参数2：终止行 参数3：起始列 参数4：终止列
		sheet.addMergedRegion(region1);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("操作日志");

		// 创建表头
		Row headRow = sheet.createRow(4);
		// 设置表头信息
		headRow.createCell(0).setCellValue("编号");
		headRow.createCell(1).setCellValue("日期");
		headRow.createCell(2).setCellValue("时间");
		headRow.createCell(3).setCellValue("管理员姓名");
		headRow.createCell(4).setCellValue("账号");
		headRow.createCell(5).setCellValue("密码");
		headRow.createCell(6).setCellValue("操作内容");

		// 遍历上面数据库查到的数据
		for (Log log : logs) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(log.getId());
			dataRow.createCell(1).setCellValue(log.getDate());
			dataRow.createCell(2).setCellValue(log.getTime());
//			dataRow.createCell(3).setCellValue(log.getAdministrator().getName());
//			dataRow.createCell(4).setCellValue(log.getAdministrator().getAccount());
//			dataRow.createCell(5).setCellValue(log.getAdministrator().getPassword());
			dataRow.createCell(6).setCellValue(log.getContent());

		}
		// 下载导出文件名
		String filename = "操作日志";
		// 设置头信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		// 一定要设置成xlsx格式
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		// 创建一个输出流
		ServletOutputStream outputStream = response.getOutputStream();
		// 写入数据
		sxssfWorkbook.write(outputStream);
		// 关闭
		outputStream.close();
		sxssfWorkbook.close();
	}
}
